package com.doit.day01;

import com.sun.media.jfxmediaimpl.HostUtils;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.List;

/**
 * 监听节点中值的变化
 */

public class WatcherDemo1 {
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {

        //创建zk的对象
        ZooKeeper zk = new ZooKeeper("linux01:2181", 2000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("就单纯的整一个监听器");
            }
        });

        List<String> list = zk.getChildren("/teacher", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    List<String> children = zk.getChildren("/teacher", this);
                    for (String child : children) {
                        System.out.println(new String(zk.getData("/teacher/"+child,null,null)));
                    }
                    System.out.println("===========分隔符===============");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        //遍历所有的节点
//        System.out.println(list);

        Thread.sleep(Integer.MAX_VALUE);

    }
}
